package com.vf.admin.client.service.pay;

import com.vf.admin.client.dao.AppPaySetupDao;
import com.vf.admin.common.Constants;
import com.vf.admin.miaodong.entity.AppPaySetup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
//public class AppPaySetupLoadBalancer implements InitializingBean {
public class AppPaySetupLoadBalancer  {

    @Autowired
    AppPaySetupDao appPaySetupDao;

    private List<AppPaySetup> alipayAppPaySetups;

    private List<AppPaySetup> wechatAppPaySetups;

    private int alipayCurrentIndex = 0;
    private int wechatCurrentIndex = 0;


    public synchronized AppPaySetup getNextAlipayAppPaySetup() {
        AppPaySetup server = alipayAppPaySetups.get(alipayCurrentIndex);
        alipayCurrentIndex = (alipayCurrentIndex + 1) % alipayAppPaySetups.size();
        return server;
    }


    public synchronized AppPaySetup getNextWechatAppPaySetup() {
        AppPaySetup server = wechatAppPaySetups.get(wechatCurrentIndex);
        alipayCurrentIndex = (wechatCurrentIndex + 1) % wechatAppPaySetups.size();
        return server;
    }

//    @Override
//    public void afterPropertiesSet() throws Exception {
//        init();
//    }

    public void init() {
        log.info("初始化后台支付配置开始...");
        this.alipayAppPaySetups = appPaySetupDao.getList(Constants.dingdan_zhiFuLeiXing_zfb);
        this.wechatAppPaySetups = appPaySetupDao.getList(Constants.dingdan_zhiFuLeiXing_wx);
        this.alipayCurrentIndex = 0;
        this.wechatCurrentIndex = 0;
        log.info("初始化后台支付配置结束！");
    }
}